import { instantiate, Node, Prefab, resources, sp } from "cc";
import { ActionBase } from "./ActionBase";
import { ResMgr } from "../../../new/common/ResMgr";
import { SkillData } from "../../../new/skill/const/SkillData";
import { BattleRoleUI } from "../../roleUI/BattleRoleUI";
import { UITriggerType } from "../UITriggerType";
import EventMgr from "../../../new/event/EventMgr";
import { EventId } from "../../../new/event/EventId";

/**
 * 动作-创建技能特效(考虑到有起手动作之类的，技能特效创建时间不固定)
 */
export class CreateEffect extends ActionBase {

    private skillData: SkillData;
    private spineNode: Node
    private spine: sp.Skeleton;
    private damageIndex: number = 0;
    private isExcute: boolean = false;
    constructor(casterUI: BattleRoleUI, targetUI: BattleRoleUI, skillData: SkillData) {
        super();
        this.casterUI = casterUI;
        this.targetUI = targetUI;
        this.skillData = skillData;
        this.init()
    }

    init() {

        let prefaburl = ResMgr.getSpinePrefab()
        resources.load(prefaburl, Prefab, null, this.onPrefabLoad.bind(this));
    }

    onPrefabLoad(err: any, data: Prefab): void {
        if (err) {
            console.log(err)
        } else {
            this.spineNode = instantiate(data);
            this.spine = this.spineNode.getComponent(sp.Skeleton);
            //加载spine动画
            let aniUrl = ResMgr.getEffectSpineAni(this.skillData.skillEffect);
            resources.load(aniUrl, sp.SkeletonData, (err, data) => {
                if (err) {
                    console.log(err)
                } else {
                    this.spine.skeletonData = data;
                }
            })
            this.spine.setEventListener(this.onAniEvent.bind(this));
            this.spine.setCompleteListener(this.onComplete.bind(this));
            if (this.isExcute) {
                this.excute()
            }
        }
    }

    /**动画事件 */
    onAniEvent(event: any, data: any) {
        if ((this.spine.animation == "att" || this.spine.animation == "skill" || this.spine.animation == "ultimateskill") && data.data.name == "back") {

        }

        switch (data.data.name) {
            case "back":
                return;
        }
        //监听到伤害帧，触发伤害计算行动
        console.log("监听到伤害帧，触发伤害计算")
        this.casterUI.UITriggerMgr.triggerByParam(UITriggerType.atkDamage_ui, { damageIndex: this.damageIndex });
        this.damageIndex++;
    }

    onComplete() {
        this.casterUI.UITriggerMgr.trigger(UITriggerType.attackEnd_ui);
        this.damageIndex++;
    }

    excute(): void {
        if (!this.spineNode) {
            this.isExcute = true;
            return;
        }
        //把特效加到场景，播放特效
        let pos;
        if (this.skillData.pos == "self") {
            pos = this.casterUI.position;
        } else {
            pos = this.targetUI.position;
        }
        EventMgr.ins.event(EventId.ADD_SKILL_EFFECT, { spine: this.spineNode, pos: pos })
        this.spine.setAnimation(0, "animation_0", false);
        //有的技能是特效播到某一帧触发伤害效果
    }
}